import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;


public class AnsweredQuestionView
{
	public AnsweredQuestionView(Question question, int answer)
	{
		itsQuestion = question;
		this.answer = answer;
	}

	public void display(JDialog parent)
	{
		// dialog
		questionDialog = new JDialog(parent, itsQuestion.getTitle(), true);
		// question label
		JLabel textLabel = new JLabel(itsQuestion.getText());
		// image
		JLabel picture = new JLabel(createImageIcon(itsQuestion.getImageFile()));
		//picture.setPreferredSize(new Dimension(150, 75));
		// radio buttons
		JRadioButton s1Btn = new JRadioButton(itsQuestion.getSelection(0)); if(answer == 0) s1Btn.setSelected(true);
		JRadioButton s2Btn = new JRadioButton(itsQuestion.getSelection(1)); if(answer == 1) s2Btn.setSelected(true);
		JRadioButton s3Btn = new JRadioButton(itsQuestion.getSelection(2)); if(answer == 2) s3Btn.setSelected(true);
		JRadioButton s4Btn = new JRadioButton(itsQuestion.getSelection(3)); if(answer == 3) s4Btn.setSelected(true);
		s1Btn.setEnabled(false);
		s2Btn.setEnabled(false);
		s3Btn.setEnabled(false);
		s4Btn.setEnabled(false);
		
		ButtonGroup group = new ButtonGroup();
		group.add(s1Btn);
		group.add(s2Btn);
		group.add(s3Btn);
		group.add(s4Btn);
		
		// panel for radio buttons and icon labels
		JPanel radioPanel = new JPanel(new GridLayout(0, 1));
		GroupLayout grp = new GroupLayout(radioPanel);
		radioPanel.setLayout(grp);
		grp.setAutoCreateGaps(true);
		grp.setAutoCreateContainerGaps(true);
		GroupLayout.SequentialGroup hGroup = grp.createSequentialGroup();
		
		// make the layout nice :)
		JLabel[] msgLabels = getMessageLabels();
		JLabel[] pictureLabels = getPictureLabels();
		hGroup.addGroup(grp.createParallelGroup().addComponent(s1Btn).addComponent(s2Btn).addComponent(s3Btn).addComponent(s4Btn));
		hGroup.addGroup(grp.createParallelGroup().addComponent(msgLabels[0]).addComponent(msgLabels[1]).addComponent(msgLabels[2]).addComponent(msgLabels[3]));
		hGroup.addGroup(grp.createParallelGroup().addComponent(pictureLabels[0]).addComponent(pictureLabels[1]).addComponent(pictureLabels[2]).addComponent(pictureLabels[3]));
		
		grp.setHorizontalGroup(hGroup);
		GroupLayout.SequentialGroup vGroup = grp.createSequentialGroup();
		
		vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).addComponent(s1Btn).addComponent(msgLabels[0]).addComponent(pictureLabels[0]));
		vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).addComponent(s2Btn).addComponent(msgLabels[1]).addComponent(pictureLabels[1]));
		vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).addComponent(s3Btn).addComponent(msgLabels[2]).addComponent(pictureLabels[2]));
		vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).addComponent(s4Btn).addComponent(msgLabels[3]).addComponent(pictureLabels[3]));
		grp.setVerticalGroup(vGroup);
		
        radioPanel.add(s1Btn);
        radioPanel.add(s2Btn);
        radioPanel.add(s3Btn);
        radioPanel.add(s4Btn);
		
        JPanel centerPanel = new JPanel();
        centerPanel.add(radioPanel, BorderLayout.LINE_START);
        centerPanel.add(picture, BorderLayout.CENTER);
        centerPanel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
		
		JButton doneBtn = new JButton("Done");
		doneBtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				questionDialog.setVisible(false);
			}
		});
		
		questionDialog.getContentPane().add(textLabel, BorderLayout.NORTH);
		questionDialog.getContentPane().add(centerPanel, BorderLayout.CENTER);
		
		JPanel doneBtnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		doneBtnPanel.add(doneBtn);
		
		questionDialog.getContentPane().add(doneBtnPanel, BorderLayout.SOUTH);
		
		questionDialog.pack();
		
		final Toolkit toolkit = Toolkit.getDefaultToolkit();
		final Dimension screenSize = toolkit.getScreenSize();
		final int x = (screenSize.width - questionDialog.getWidth()) / 2;
		final int y = (screenSize.height - questionDialog.getHeight()) / 2;
		questionDialog.setLocation(x, y);
		
		questionDialog.setVisible(true);
	}
	
	private JLabel[] getPictureLabels()
	{
		JLabel[] labels = new JLabel[4];
		for(int i = 0; i < 4; i++) labels[i] = new JLabel("");
		if(answer == itsQuestion.getAnswer())
		{
			labels[itsQuestion.getAnswer()].setIcon(createImageIcon("left-green-32.jpg"));
		}
		else
		{
			labels[itsQuestion.getAnswer()].setIcon(createImageIcon("left-green-32.jpg"));
			labels[answer].setIcon(createImageIcon("left-red-32.jpg"));
		}
		return labels;
	}

	private JLabel[] getMessageLabels()
	{
		JLabel[] labels = new JLabel[4];
		for(int i = 0; i < 4; i++) labels[i] = new JLabel("");
		labels[answer].setText("your answer");
		if(answer != itsQuestion.getAnswer())
		{
			labels[itsQuestion.getAnswer()].setText("correct answer");
		}
		return labels;
	}

	private ImageIcon createImageIcon(String filename)
	{
		return new ImageIcon(filename);
	}
	
	
	private int answer;
	private JDialog questionDialog;
	private Question itsQuestion;
}
